<template>
  <div id="login">
    <div class="login">
      <div class="logo">
        <img :src="Logo"/>
        <span>MK System</span>
      </div>
<!--      表单-->
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="账号" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        
        
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import Logo from  '../assets/images/logo.jpg'
  import BG from '../assets/images/bg.jpg'
  import { mapActions } from 'vuex'

  export default {
    name: "Login",
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'))
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass')
          }
          callback()
        }
      }
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入账号'))
        } else
          callback()
      }
      return {
        Logo,
        BG,
        ruleForm: {
          pass: '',
          checkPass: '',
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      ...mapActions(['loginAC']),
      submitForm (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // TODO：登录功能
            const obj = {
              username: this.ruleForm.checkPass,
              password: this.ruleForm.pass,
            }
            this.loginAC(obj).then(res => {
              if (res.code===400) this.$message.error('请求失败,请重新登录')
              else if (res.code===200) this.$router.push('/index')
            })
          } else {
            console.log('error submit!!')
            this.$message.error('请输入账号和密码')
            return false
          }
        })
      },
      resetForm (formName) {
        this.$refs[formName].resetFields()
      }
    },
    
  }
</script>

<style lang="less" scoped>
#login {
  height: 100%;
    .login {
    width: 360px;
    height: 320px;
    background-color: rgba(0,0,0,.1);
    border: 1px solid white;
    border-radius: 5px;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
    padding: 30px;
    .logo {
      line-height: 50px;
      text-align: center;
      border-bottom: 1px solid rgba(255,255,255,.3);
      padding-bottom: 10px;
      margin-bottom: 10px;
      img {
        width: 50px;
        border-radius: 50%;
        vertical-align: middle;
        margin: 0 20px;
      }
      span {
        color: white;
      }
    }
    .el-form {
      width: auto;
      .el-form-item {
      }
    }
    
  }
}
</style>